package com.fansin.hyperledger.fabric.java;

import com.fansin.hyperledger.fabric.java.store.SampleStore;
import lombok.extern.slf4j.Slf4j;
import org.hyperledger.fabric.sdk.HFClient;
import org.hyperledger.fabric.sdk.exception.CryptoException;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.security.CryptoSuite;

import java.io.File;
import java.lang.reflect.InvocationTargetException;

/**
 * <p>Title: FabricManager</p>
 * <p>Description: </p>
 *
 * @author zhaofeng
 * @version 1.0
 * @date 18-8-23
 */
@Slf4j
public class FabricManager {

    private FabricManager(){
    }

    public static class FabricHolder{

        static SampleStore sampleStore = new SampleStore(new File(System.getProperty("java.io.tmpdir") + "/HFCSample.properties"));
        static HFClient client = HFClient.createNewInstance();
        static{
            try {
                client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
            } catch (CryptoException e) {
                e.printStackTrace();
            } catch (InvalidArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }

    }

    public static HFClient newClient(){
        return FabricHolder.client;
    }


    public static SampleStore getSampleStore(){
        return FabricHolder.sampleStore;
    }


}
